package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class MyTeamServiceImpl implements MyTeamService{
    protected static final Logger LOG = LoggerFactory.getLogger(MyTeamServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private MyTeamMapper myTeamMapper;

    @Override
    public MyTeam create(MyTeam item) {
         myTeamMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return myTeamMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(MyTeam item) {
        return myTeamMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public MyTeam get(Integer id) {
        return myTeamMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<MyTeam> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,MyTeam item){
       MyTeamExample  example = new MyTeamExample();
       MyTeamExample.Criteria criteria = example.createCriteria();
       if(item.getInvestId() != null ){
            criteria.andInvestIdEqualTo(item.getInvestId());
        }
        if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLsatUpdateTime() != null ){
            criteria.andLsatUpdateTimeEqualTo(item.getLsatUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<MyTeam> list = this.myTeamMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<MyTeam> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,MyTeam item){
       MyTeamExample  example = new MyTeamExample();
       MyTeamExample.Criteria criteria = example.createCriteria();
       if(item.getInvestId() != null ){
            criteria.andInvestIdEqualTo(item.getInvestId());
        }
        if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLsatUpdateTime() != null ){
            criteria.andLsatUpdateTimeEqualTo(item.getLsatUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<MyTeam> list = this.myTeamMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,MyTeam item){
        MyTeamExample  example = new MyTeamExample();
        MyTeamExample.Criteria criteria = example.createCriteria();
        if(item.getInvestId() != null ){
            criteria.andInvestIdEqualTo(item.getInvestId());
        }
        if(item.getLevel() != null ){
            criteria.andLevelEqualTo(item.getLevel());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getLimits() != null ){
            criteria.andLimitsEqualTo(item.getLimits());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLsatUpdateTime() != null ){
            criteria.andLsatUpdateTimeEqualTo(item.getLsatUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<MyTeam> list = this.myTeamMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(myTeam -> JSONObject.parseObject(JSONObject.toJSONString(myTeam))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public MyTeam getByUserId(Integer investId, Integer userId) {
        return  this.myTeamMapper.getByUserId( investId, userId);
    }

}
